package longrise.phone.com.bjjt_henan.compensate;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.android.util.IntenetUtil;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.StringUtils;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.BDLocationManager;
import longrise.phone.com.bjjt_henan.Utils.PermissionHelper;

/**
 * 进入快处快赔的安全提示，Created by Administrator on 2016/5/12.
 */
public class SafeWarningView extends LFView implements View.OnClickListener, BDLocationListener {
    private Context context = null;
    private View view = null;
    private Button bt_safewarining_ensure;
    private BaseApplication mApplication;
    private LinearLayout hn_btn_back;
    private TextView tv_title;
    private Dialog dialog;
    private boolean isSuccess = false;//定位是否成功
    private boolean isAgain = false;//是否重新定位

    private BDLocationManager manager;

    public SafeWarningView(Context c) {
        super(c);
        this.context = c;
        this.mApplication = ((MainActivity) c).getAppData();
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_safewarning, null);
        bt_safewarining_ensure = (Button) view.findViewById(R.id.bt_safewarining_ensure);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("安全提示");

        if (manager == null) {
            manager = new BDLocationManager(context);
            manager.setLocationListener(this);
        }
        // 判断是否有网络连接
        if (IntenetUtil.getNetworkState(context) == IntenetUtil.NETWORN_NONE) {
            Toast.makeText(context, "没有网络连接", Toast.LENGTH_SHORT).show();
            return;
        }
        manager.startLocation();
        dialog = UiUtil.showProcessDialog(context, "获取位置中...");
    }

    private void regEvent(boolean b) {
        if (null != bt_safewarining_ensure) {
            bt_safewarining_ensure.setOnClickListener(b ? this : null);
        }

        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_safewarining_ensure:
                //判断是否有定位权限
                if (!PermissionHelper.checkLocPermissions(context)) {
                    isSuccess = false;
                    return;
                }
                if (!isSuccess) {//定位失败则重新定位
                    isAgain = true;
                    manager.bdLocation();
                    if (dialog == null) {
                        dialog = UiUtil.showProcessDialog(context, "获取位置中...");
                    } else {
                        dialog.show();
                    }
                    return;
                }
                PromptView promptview = new PromptView(context);
                FrameworkManager.getInstance().showNewForm(context, promptview);
                break;
            case R.id.hn_btn_back:
                closeForm();
                OnDestroy();
                break;
        }
    }

    /**
     * 定位回调方法
     */
    @Override
    public void onReceiveLocation(BDLocation location) {
        try {
            if (null == view) {
                return;
            }
            if (null != location) {
                String cityCode = location.getCityCode();
                if (!StringUtils.isEmpty(cityCode)) {
                    String latitude = String.valueOf(location.getLatitude());
                    String longitude = String.valueOf(location.getLongitude());
                    // 经纬度
                    mApplication.setLatitude(latitude);
                    mApplication.setLongitude(longitude);
                    // 详细地址
                    mApplication.setAddress(location.getAddrStr());
                    // 获取省和市等信息
                    mApplication.setAddressbean(location.getAddress());

                    getAreaId(latitude, longitude);
                    return;
                }
            }
            if (null != dialog) {
                dialog.cancel();
            }
            if (isAgain) {
                UiUtil.showToast(context, "定位异常，请检查定位权限和网络连接后重新尝试");
            }
        } finally {
            // 停止定位
            if (manager != null) {
                manager.stop();
            }
        }
    }

    private void getAreaId(String latitude, String longitude) {
        String userflag = mApplication.getKckpName();
        String token = mApplication.getToken();
        ApiImpI.getInstance(context).getAreaId(longitude, latitude, userflag, token,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        if (null != dialog) {
                            dialog.cancel();
                        }
                        if (!TextUtils.isEmpty(error)) {
                            UiUtil.showToast(context, error);
                        }
                    }

                    @Override
                    public void onSuccess(Object result) {
                        if (null != dialog) {
                            dialog.cancel();
                        }
                        if (null == view) {
                            return;
                        }
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        if (TextUtils.equals(restate, "1")) {
                            EntityBean dataBean = bean.getBean("data");
                            if (null != dataBean) {
                                EntityBean posBean = dataBean.getBean("position");
                                if (null != posBean) {
                                    String areaId = posBean.getString("areaid");
                                    String address = posBean.getString("mapaddress");
                                    mApplication.setAreaid(areaId);
                                    mApplication.setAddress(address);
                                    isSuccess = true;
                                    if (isAgain) {
                                        PromptView promptview = new PromptView(context);
                                        FrameworkManager.getInstance().showNewForm(context, promptview);
                                        isAgain = false;//重置参数
                                        manager.ondestroty();
                                        manager = null;
                                    }
                                    return;
                                }
                            }
                        }
                        String redes = bean.getString("redes");
                        Toast.makeText(context, redes, Toast.LENGTH_SHORT).show();
                    }
                });
    }

    @Override
    public void OnDestroy() {
        dialog = null;
        view = null;
        regEvent(false);
        isSuccess = false;
        isAgain = false;
        if (null != manager) {
            manager.ondestroty();
            manager = null;
        }
        super.OnDestroy();
    }
}
